import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/student',
    name: 'student',
    redirect: '/studentHome',
    component: () => import('../views/student/student.vue'),
    children: [
      {
        path: '/studentHome',
        name: 'studentHome',
        redirect: '/student/basicContent',
        component: () => import('../views/student/student_home.vue'),
        children: [
          {
            path: '/student/changeInfo',
            name: "studentChangeInfo",
            component: () => import('../components/student/home/changeInfo.vue')
          },

          {
            path: '/student/basicContent',
            name: "studentBasicContent",
            component: () => import('../components/student/home/basicContent/basicContent.vue')
          }
        ]
      },
      {
        path: '/student/courseManagement',
        name: 'studentCourseMangement',
        redirect: '/student/courseMangement/courseInfo',
        component: () => import('../views/student/student_coursemanagement.vue'),
        children: [
          {
            path: '/student/courseMangement/courseInfo',
            name: 'studentCourseInfo',
            component: () => import('../components/student/courseManagement/courseInfo/courseInfo.vue')
          },
          {
            path: '/student/courseMangement/courseResource',
            name: 'studentCourseResource',
            component: () => import('../components/student/courseManagement/courseResource/courseResource.vue')
          },
          {
            path: '/student/courseMangement/courseHomework',
            name: 'studentCourseHomework',
            component: () => import('../components/student/courseManagement/courseHomework/courseHomework.vue')
          },
          {
            path: "/student/courseHomework/viewCorrection",
            name: "viewCorrection",
            component: () => import('../components/student/courseManagement/courseHomework/viewCorrection.vue')
          }
        ]
      }

    ]
  },
  {
    path: '/teacher',
    name: 'teacher',
    redirect: '/teacherHome',
    component: () => import('../views/teacher/teacher.vue'),
    children: [
      {
        path: '/teacherHome',
        name: 'teacherHome',
        redirect: '/teacher/basicContent',
        component: () => import('../views/teacher/teacher_home.vue'),
        children: [
          {
            path: '/teacher/changeInfo',
            name: "teacherChangeInfo",
            component: () => import('../components/teacher/home/changeInfo.vue')
          },
          {
            path: '/teacher/basicContent',
            name: "teacherBasicContent",
            component: () => import('../components/teacher/home/basicContent/basicContent.vue')
          }
        ]
      },
      {
        path: '/teacher/courseManagement',
        name: 'teacherCourseMangement',
        redirect: '/teacher/courseMangement/courseInfo',
        component: () => import('../views/teacher/teacher_coursemanagement.vue'),
        children: [
          {
            path: '/teacher/courseMangement/courseInfo',
            name: 'teacherCourseInfo',
            component: () => import('../components/teacher/courseManagement/courseInfo/courseInfo.vue')
          },
          {
            path: '/teacher/courseMangement/courseResource',
            name: 'teacherCourseResource',
            component: () => import('../components/teacher/courseManagement/courseResource/courseResource.vue')
          },
          {
            path: '/teacher/courseMangement/courseHomework',
            name: 'teacherCourseHomework',
            redirect: '/teacher/courseMangement/homeworkHome',
            component: () => import('../components/teacher/courseManagement/courseHomework/courseHomework.vue'),
            children: [
              {
                path: '/teacher/courseMangement/homeworkHome',
                name: 'homeworkHome',
                component: () => import('../components/teacher/courseManagement/courseHomework/homework_home.vue')
              },
              {
                // 某一个作业的统计信息
                path: '/teacher/courseMangement/homeworkStatistics',
                name: 'homeworkStatistics',
                component: () => import('../components/teacher/courseManagement/courseHomework/homeworkStatistics.vue')
              },
              {
                // 某一个作业的提交情况
                path: '/teacher/courseMangement/submitStatus',
                name: 'homeworkSubmitStatus',
                component: () => import('../components/teacher/courseManagement/courseHomework/submitStatus.vue')
              },
              {
                // 某一个学生的作业情况
                path: '/teacher/courseMangement/studentStatistics',
                name: 'studentStatistics',
                component: () => import('../components/teacher/courseManagement/courseHomework/studentStatistics/studentStatistics.vue')
                // children: [
                //   {
                //     path: '/teacher/courseMangement/studentStatistics/concreteContent',
                //     name: 'concreteContent',
                //     components: () => import('../components/teacher/courseManagement/courseHomework/studentStatistics/concreteContent.vue')
                //   }
                // ]
              }
            ]
          }
        ]
      }
    ]
  },

  {
    path: '/admin',
    name: 'adminHome',
    redirect: '/admin/teacherInfo',
    component: () => import('../views/administrator/admin_home.vue'),
    children: [

      {
        path: '/admin/teacherInfo',
        name: 'teacherInfo',
        component: () => import('../components/administrator/teacherInfo.vue')
      },
      {
        path: '/admin/stuInfo',
        name: 'stuInfo',
        component: () => import('../components/administrator/stuInfo.vue')
      },
      {
        path: '/admin/courseInfo',
        name: 'courseInfo',
        component: () => import('../components/administrator/courseInfo.vue')
      },
      {
        path: '/admin/addCourse',
        name: 'addCourse',
        component: () => import('../components/administrator/addCourse.vue')
      }
    ]
  },

  // 这些是模板的路由
  // {
  //   path: '/common/table',
  //   name: 'TableTemplate',
  //   component: () => import('../common/tableTemplate.vue')
  // },
  // {
  //   path: '/common/navigation',
  //   name: 'navigation',
  //   component: () => import('../common/navigationTemplate.vue')
  // },
  // {
  //   path: '/common/topBar',
  //   name: 'topBarTemplate',
  //   component: () => import('../common/topBarTemplate.vue')
  // },
  // {
  //   path: '/common/courseTable',
  //   name: 'courseTable',
  //   component: () => import('../common/courseTableTemplate.vue')
  // },
  // {
  //   path: '/common/fileList',
  //   name: 'fileList',
  //   component: () => import('../common/fileListTemplate.vue')
  // },
  // {
  //   path: '/common/transfer',
  //   name: 'transfer',
  //   component: () => import('../common/transferTemplate.vue')
  // },
  // {
  //   path: '/common/courseList',
  //   name: 'courseList',
  //   component: () => import('../common/courseListTemplate.vue')
  // },
  // {
  //   path: '/common/calendar',
  //   name: 'calendar',
  //   component: () => import('../common/calendarTempale.vue')
  // },
  // {
  //   path: '/common/courseAnnouncement',
  //   name: 'courseAnnouncement',
  //   component: () => import('../common/courseAnnouncementTemplate.vue')
  // },
  // {
  //   path: '/common/personProfile',
  //   name: 'personProfile',
  //   component: () => import('../common/personProfileTemplate.vue')
  // },
  // {
  //   path: '/common/inputBox',
  //   name: 'inputBox',
  //   component: () => import('../common/inputboxTemplate.vue')
  // },
  // {
  //   path: '/common/messageAlert',
  //   name: 'messageAlert',
  //   component: () => import('../common/messageAlertTemplate.vue')
  // },
  // {
  //   path: '/common/dialog',
  //   name: 'dialog',
  //   component: () => import('../common/dialogTemplate.vue')
  // },

  // // 这里是云对象存储模板
  // {
  //   path: '/mybucket/mybucketTemplate',
  //   name: 'mybucket',
  //   component: () => import('../mybucket/mybucketTemplate.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 设定全局守卫
router.beforeEach((to, from, next) => {
  // console.log('加载 ' + to.name)
  if (Vue.cookie.get('id') != null) {
    // if (store.state.id != "") {
    /*  若已登录，则替换至对应路由主页           */
    if (to.name === 'Login') {
      // 选择对应跳转
      let toPath = ''
      switch (Vue.cookie.get('roleName')) {
        // switch (store.state.roleName) {
        case "student":
          toPath = '/student'
          break;
        case "admin":
          toPath = '/admin'
          break;
        case "teacher":
          toPath = '/teacher'
          break;
        default:
          console.log(store.state.roleName + "身份未知");
      }
      next(toPath)

      // next() //临时
    } else {
      next()
    }
  } else {
    // 未登陆过
    if (to.name === 'Login') {
      next()
    } else {
      // next() //临时

      alert("用户未登录或登录已超时!")

      next(
        {
          name: 'Login',
          // query: { redirect: to.fullPath }
        }
      )
    }
  }
})

export default router
